package com.demo.util;

import com.baidu.aip.face.AipFace;
import com.demo.context.BaiDuContext;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author yl
 * @description 百度人脸工具类
 * @date 2020/11/17 15:28
 */
@Component
public class BaiDuFaceUtil {

    @Value("${baidu.APP_ID}")
    public String clientId="22993989";
    @Value("${baidu.API_KEY}")
    public String apiKey="Y449f2fR1aA3fQnYn3NYrPxU";
    @Value("${baidu.SECRET_KEY}")
    public String secretKey="NfNC1DsepC2VEECecrObah3HUtQNVHIC";

    AipFace client=null;

    public BaiDuFaceUtil(){
        // 初始化一个AipFace
        client = new AipFace(clientId, apiKey, secretKey);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(6000);
    }

    /**
     * 检测人脸信息（如果不是人脸，result =null）
     * @param imagebase64
     * @return
     */
    public String detect(String imagebase64){
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("max_face_num", "5");
        // 人脸检测
        JSONObject res = client.detect(imagebase64, BaiDuContext.imageType, options);
        return res.toString();
    }
    public String search(String imagebase64){
        HashMap<String, String> options = new HashMap<>();
//        map.put("image", imagebase64);
        options.put("liveness_control", "NORMAL");
//        options.put("group_id_list", "group_repeat,group_233");
//        options.put("image_type", "FACE_TOKEN");
        options.put("quality_control", "LOW");
        String group_id_list=BaiDuContext.groupId+",group_233";
        JSONObject res = client.search(imagebase64, BaiDuContext.imageType, group_id_list, options);
        return res.toString();
    }

    /**
     * 人脸注册
     * @param imagebase64
     * @return
     */
    public String add(String imagebase64){
        HashMap<String, String> options = new HashMap<>();
//        options.put("image", imagebase64);
//        options.put("group_id", "group_repeat");
//        options.put("user_id", "user1");
        options.put("user_info", "abc");
        options.put("liveness_control", "NORMAL");
//        options.put("image_type", "FACE_TOKEN");
        options.put("quality_control", "LOW");
        JSONObject res = client.addUser(imagebase64, BaiDuContext.imageType, BaiDuContext.groupId, BaiDuContext.userId, options);
        return res.toString();
    }

//    public String match(Map<String,Object> map){
//
//        String param = GsonUtils.toJson(map);
//        JSONObject res = client.match(param);
//        return res.toString();
//    }
}
